package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.domain.SystemRule;
import com.foreverwin.jzymes.common.mapper.SystemRuleMapper;
import com.foreverwin.jzymes.common.service.ISystemRuleAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SystemRuleAdviceServiceImpl implements ISystemRuleAdviceService {

    @Autowired
    SystemRuleMapper systemRuleMapper;

    /**
     * 查询系统规则
     *
     * @param site
     * @param ruleName
     * @return
     */
    public String existSystemRule( String site, String ruleName ){
        String systemRule = findSystemRule( site, ruleName );
        if( systemRule == null || StringUtils.isBlank( systemRule ) ){
            throw BusinessException.build("common.systemRuleNotExist", new String[]{ site, ruleName } );
        }
        return systemRule;
    }

    /**
     * 查询系统规则
     *
     * @param site
     * @param ruleName
     * @return
     */
    public String findSystemRule( String site, String ruleName ){
        String handle = BOHandleUtil.buildSystemRuleHanlde( site, ruleName );
        SystemRule systemRule = systemRuleMapper.selectSystemRuleById( handle );
        if( systemRule == null || StringUtils.isBlank( systemRule.getRuleValue() ) ){
            return null;
        }
        return  systemRule.getRuleValue();
    }

    /**
     * 查询系统规则
     *
     * @param site
     * @param ruleName
     * @param defaultValue
     * @return
     */
    public String findSystemRule( String site, String ruleName, String defaultValue ){
        String systemRule = findSystemRule( site, ruleName );
        if( systemRule == null || StringUtils.isBlank( systemRule ) ){
            return defaultValue;
        }
        return systemRule;
    }
}
